package cn.edu.sdu.order.backend.service.utils;

import cn.edu.sdu.order.backend.model.Order;
import cn.edu.sdu.order.backend.vo.GoodsVO;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class GoodsUtils {

    public static GoodsVO convertToVo(Order order) {
        GoodsVO goodsVO = new GoodsVO();
        goodsVO.setId(order.getId());
        goodsVO.setGoodsCode(order.getGoodsCode());
        goodsVO.setFpgaCode(order.getFpgaCode());
        goodsVO.setState(order.getState());
        List<String> beginningList = Arrays.asList(order.getBeginning().split(","));
        List<Double> beginning = stringToDouble(beginningList);
        goodsVO.setBeginning(beginning);
        List<String> endingList = Arrays.asList(order.getDestination().split(","));
        List<Double> destination = stringToDouble(endingList);
        goodsVO.setDestination(destination);
        goodsVO.setTemperature(order.getTemperature());
        goodsVO.setHumidity(order.getHumidity());
        List<String> trackList = Arrays.asList(order.getTrack().split(","));
        List<Double> track = stringToDouble(trackList);
        goodsVO.setTrack(track);
        goodsVO.setOwner(order.getOwner());
        goodsVO.setDeleted(order.getDeleted());
        return goodsVO;
    };

    private static List<Double> stringToDouble(List<String> strings) {
        double b0 = Double.parseDouble(strings.get(0));
        double b1 = Double.parseDouble(strings.get(1));
        List<Double> beginning = Stream.of(0.0, 0.0).collect(Collectors.toList());
        beginning.set(0, b0);
        beginning.set(1, b1);;
        return beginning;
    }
}
